﻿/*
COPYRIGHT (C) 2008 EPISERVER AB

THIS FILE IS PART OF WIKIX.
WIKIX IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

WIKIX IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH WIKIX. IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/

using System;
using System.Web.UI;
using EPiServer.DynamicContent;
using EPiServer.Core;
using System.Text;
using EPiServer.Research.Wiki.Properties;

namespace EPiServer.Research.Wiki.DynamicContent
{
    public abstract class WikiElement : EPiServer.UserControlBase, IDynamicContent
    {
        protected abstract string GetAscxUrl();

        public WikiElement()
        {
            PropertyAccessLevel pal = new PropertyAccessLevel();
            pal.Name = "Minimum access required";
            properties.Add(pal);
            CreateProperties();
        }

        protected virtual void CreateProperties() { }

        public virtual void SetProperties(PropertyDataCollection pdc)
        {
        }

        #region IDynamicContent Members

        public Control GetControl(PageBase hostPage)
        {
            if ((properties["Minimum access required"] as PropertyAccessLevel).CheckAllowed(CurrentPage))
            {
                WikiElement we=hostPage.LoadControl(GetAscxUrl()) as WikiElement;
                we.SetProperties(properties);
                return we;
            }
            else return new System.Web.UI.WebControls.PlaceHolder();
        }

        protected PropertyDataCollection properties = new PropertyDataCollection();

        public EPiServer.Core.PropertyDataCollection Properties
        {
            get {
                return properties;
            }
        }

        public string Render(PageBase hostPage)
        {
            throw new NotImplementedException();
        }

        public bool RendersWithControl
        {
            get { return true; }
        }

        static private string DecodeFrom64(string encodedData)
        {

            try
            {
                byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
                string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);

                return returnValue;

            }
            catch (Exception e)
            {
                throw;
            }


        }

        static private string EncodeTo64(string toEncode)
        {

            byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);

            string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);

            return returnValue;

        }

        public string State
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                foreach (PropertyData pd in Properties)
                {
                    sb.Append(EncodeTo64(pd.ToString()) + "|"); //TODO: base 64 encode
                }
                
                return sb.ToString();
            }
            set
            {
                string[] parts = value.Split('|');
                for (int i = 0; i < properties.Count; i++)
                {
                    if(parts.Length>i)   properties[i].ParseToSelf(DecodeFrom64(parts[i]));
                }
            }
        }

        #endregion
    }
}
